package com.bite.spring.trans.controller;

import com.bite.spring.trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RequestMapping("/trans")
@RestController
public class TransController {
    @Autowired
    private UserService userService;

    @Transactional
    @RequestMapping("/r1")
    public Boolean r1(String userName, String password) {
        Integer i = 0;

        i = userService.insert(userName, password);
        log.info("影响的行数为：" + i);
        return i == 1;
    }

    @Transactional
    @RequestMapping("/r2")
    public Boolean r2(String userName, String password) {
        Integer i = 0;

        i = userService.insert(userName, password);
        int w = 1 / 0; // 抛出运行时异常就会自动终止
        log.info("影响的行数为：" + i);
        return i == 1;
    }

    @Transactional
    @RequestMapping("/r3")
    public Boolean r3(String userName, String password) throws IOException {
        Integer i = 0;

        i = userService.insert(userName, password);
        if (true) {
            // 如果抛出不是运行时异常 那么就不会自动回滚
            throw new IOException();
        }
        log.info("影响的行数为：" + i);
        return i == 1;
    }

    @Transactional(rollbackFor = Exception.class) // 如果想要不是运行时异常就回滚 那么需要配置
    @RequestMapping("/r4")
    public Boolean r4(String userName, String password) throws IOException {
        Integer i = 0;

        i = userService.insert(userName, password);
        if (true) {
            throw new IOException();
        }
        log.info("影响的行数为：" + i);
        return i == 1;
    }

    @Transactional
    @RequestMapping("/r5")
    public Boolean r5(String userName, String password) {
        Integer i = 0;
        try {
            int p = 1 / 0; // 自己处理了 所以就不回滚了
        } catch (Exception e) {
            e.printStackTrace();
        }
        i = userService.insert(userName, password);
        log.info("影响的行数为：" + i);
        return i == 1;
    }

    @Transactional
    @RequestMapping("/r6")
    public Boolean r6(String userName, String password) {
        Integer i = 0;
        try {
            int p = 1 / 0;
        } catch (Exception e) {
            throw e; // 虽然处理了 但是还抛出 所以还是要回滚
        }
        i = userService.insert(userName, password);
        log.info("影响的行数为：" + i);
        return i == 1;
    }

    @Transactional
    @RequestMapping("/r7")
    public Boolean r7(String userName, String password) {
        Integer i = 0;
        try {
            int p = 1 / 0;
        } catch (Exception e) {
            // 手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        i = userService.insert(userName, password);
        log.info("影响的行数为：" + i);
        return i == 1;
    }
}
